package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.ExtensionWorker;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.ExtensionService;
import com.yami.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@RequestMapping("/extension")
public class ExtensionController {
    private final ExtensionService extensionService;

    private final UserService userService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<ExtensionWorker>> getExtensionWorker(PageParam<ExtensionWorker> page,ExtensionWorker extensionWorker){
        IPage<ExtensionWorker> extensionWorkerIPage = null;
        extensionWorkerIPage = extensionService.pageExtensionWork(page,extensionWorker);
        return ResponseEntity.ok(extensionWorkerIPage);
    }

    /**
     * 修改
     */
    @PostMapping("/updateExtension")
    public ResponseEntity<String> updateExtension(@RequestBody ExtensionWorker extension){
        Long promotionId = extension.getPromotionId();
        String status = extension.getStatus();
        String result = extensionService.updateExtension(promotionId,status);
        return ResponseEntity.ok(result);
    }

    /**
     * 推广人启用或禁用
     */
    @PostMapping("/updateExtensionWorker")
    public ResponseEntity<String> updateExtensionWorker(@RequestBody ExtensionWorker extensionWorker){
        String result = "success";
        String worker = extensionWorker.getExtensionWorker();
        Long promotionId = extensionWorker.getPromotionId();
        if(worker!=null&&!"".equals(worker)){
            if("1".equals(worker)){
                ExtensionWorker extensionWorker1 = extensionService.getById(promotionId);
                extensionWorker1.setExtensionWorker("1");
                extensionService.saveOrUpdate(extensionWorker1);
            }else if("0".equals(worker)) {
                ExtensionWorker extensionWorker1 = extensionService.getById(promotionId);
                extensionWorker1.setExtensionWorker("0");
                extensionService.saveOrUpdate(extensionWorker1);
            }
        }
        return ResponseEntity.ok(result);
    }


}
